import request from "@/utils/request"

export function ajax(method, url, params) {
  return request({
    url: url,
    method: method,
    data: {
      receiverName: params.receiverName,
      receiverMobile: params.receiverMobile,
      receiverProvince: params.receiverProvince,
      receiverCity: params.receiverCity,
      receiverDistrict: params.receiverDistrict,
      receiverAddress: params.receiverAddress,
      receiverZip: params.receiverZip
    }
  })
}

/*添加购物车*/
export function login(username, password) {
  return request({
    url: `/user/login`,
    method: 'POST',
    data: {
      username,
      password
    }
  })
}

export function getUserInfo() {
  return request({
    url: `/user`,
    method: 'GET'
  })
}

export function getCartCount() {
  return request({
    url: `/carts/products/sum`,
    method: 'GET'
  })
}

export function logout() {
  return request({
    url: `/user/logout  `,
    method: 'POST'
  })
}

export function getCarts() {
  return request({
    url: `/carts`,
    method: 'GET'
  })
}

/*删除购物车*/
export function delProduct(item) {
  return request({
    url: `/carts/${item.productId}`,
    method: 'delete'
  })
}

// 控制全选功能
export function toggleAll(isAllChecked) {
  let url = isAllChecked ? '/carts/unSelectAll' : '/carts/selectAll';
  return request({
    url,
    method: 'put'
  })
}

// 更新购物车数量和购物车单选状态

export function updateCart(productId, quantity, selected) {
  return request({
    url: `/carts/${productId}`,
    method: 'put',
    data: {
      quantity,
      selected
    }
  })
}

export function getAddressList() {
  return request({
    url: `/shippings`,
    method: 'get',
  })
}

export function orderSubmit(id) {
  return request({
    url: '/orders',
    method: 'POST',
    data: {
      shippingId: id
    }
  })
}

//pageSize, pageNum
export function getOrderList(current = 1, pageSize = 2) {
  return request({
    url: `/orders`,
    method: 'GET',
    params: {
      pageNum: current,
      pageSize
    }
  })
}

export function getOrderDetail(orderNo) {
  return request({
    url: `/orders/${orderNo}`,
    method: 'GET',
  })
}


export function paySubmit(order) {
  return request({
    url: `/pay`,
    method: 'POST',
    data: {
      orderId: order.orderId,
      orderName: order.orderName,
      amount: order.amount,
      payType: order.payType,
    }
  })
}

export function getOrderState(orderId) {
  return request({
    url: `/orders/${orderId}`,
    method: 'get',
  })
}
